#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <queue>
#include <map>
#include <unordered_map>
#include <vector>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstring>
#include <set>
#include <unordered_set>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
const int N = 3e5 + 10;
string s;
string l,r;
int st[15][15];
int n;
char b[15];
int f;


int main()
{
	int t;
	cin >> t;
	while (t--)
	{
		f = 0;
		cin >> s >> n >> l >> r;
		for(int i=0,j=0;i<n;i++)
		{
			int cnt=0;
			int num=r[i]-l[i]+1;
			while(j<s.size() && cnt<num)
			{
				if(s[j]<l[i] || s[j]>r[i])
				{
					j++;
					continue;
				}
				else if(st[i][s[j]-'0']==0)
				{
					st[i][s[j]-'0']=1;
					cnt++;
				}
				j++;
			}
			if(j==s.size())
			{
				for(int k=l[i]-48;k<=r[i]-48;k++)
				{
					if(st[i][k]==0)
					{
						f=1;
						break;
					}
				}
			}
		}
		memset(st,0,sizeof(st));
		if(f) cout<<"YES"<<endl;
		else cout<<"NO"<<endl;
	}

	return 0;
}

